import glob
import re
import os

snips = glob.glob('./codeCompleteSnip/*.snip')
# print(snips)

def getSnip(idx,name,code):
    return """
            <entry{idx}>
				<LANGUAGE>
					<str>
						<![CDATA[C/C++]]>
					</str>
				</LANGUAGE>
				<NAME>
					<str>
						<![CDATA[{name}]]>
					</str>
				</NAME>
				<CODE>
					<str>
						<![CDATA[{code}]]>
					</str>
				</CODE>
			</entry{idx}>
    """.format(**{"idx":idx,"name":name,"code":code})

autoComple = ''
for i,snip in enumerate(snips):
    with open(snip,'r') as f:
         autoComple+=getSnip(str(i+1),os.path.basename(snip).split(".")[0],f.read().replace('\n','\\\\n') )

autoComple = """
<auto_complete>
{}
</auto_complete>
""".format(autoComple)

os.system("cp ~/.config/codeblocks/default.conf ~/.config/codeblocks/default.conf.bak")
print("备份: ~/.config/codeblocks/default.conf \n --> ~/.config/codeblocks/default.conf.bak")

newFILE=''
with open(os.path.join(os.path.expanduser("~"),'.config/codeblocks/default.conf'),'r') as f:
    newFILE = re.sub('<auto_complete>[\s\S]+<\/auto_complete>',autoComple,f.read())
with open(os.path.join(os.path.expanduser("~"),'.config/codeblocks/default.conf'),'w') as f:
    f.write(newFILE)

print('写入完毕!')
